/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commons;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


/**
 * 
 * This class handles all the Input and Output this research uses
 * 
 * 
 * <p />
 * Project VaR<br /> 
 * IOhandler.java created Aug 30, 2011
 * <p />
 * Copyright &copy 2011 SemLab
 * @author    <a href="mailto:dewinter@semlab.nl">M.R. de Winter</a>  
 * @version   $Revision: 1.3 $, $Date: 2011/09/19 12:45:56 $
 */
public class IOhandler  
{
   static List<FileWriter> aFileWriters = new ArrayList<FileWriter>();
   static List<Scanner> aScanners = new ArrayList<Scanner>() ;
   static int tellerW =-1;
   static int tellerS =-1;
   
   
   /**
    * Opens a writer to write in the input folder.
    * The created file will be a .csv.
    * 
    * @param aDestination the folder path in the input folder
    * @throws IOException
    */
   public void openInputWriter(String aDestination) throws IOException
   {
      FileWriter aFileWriter= new FileWriter(new File(aDestination));
      aFileWriters.add(aFileWriter);
      tellerW++;
      
   }
   /**
    * Opens a writer to write in the output folder.
    * The created file will be a .csv.
    * 
    * @param aDestination the folder path in the output folder
    * @throws IOException
    */
   public void openOutputWriter(String aDestination) throws IOException
   {
      FileWriter aFileWriter= new FileWriter(new File(aDestination));
      aFileWriters.add(aFileWriter);
      tellerW++;
   }
   
   /**
    * Fills the arraylist of all the files associated to a patient number.
    * 
    * @param root the directory which has to be searched 
    * @param datOnly the ArrayList to be included
    * @param aPatientNumber a patients number
    * @param aFoundNumber if the patient has a directory
    */   
   public static void searchForDatFiles(File root, List<File> datOnly, String aPatientNumber,Boolean aFoundNumber, Boolean onlyDirectory)
{
    if(root == null || datOnly == null) return; //just for safety 
    
    
    
   
    if(root.isDirectory())
    {    
        //System.out.println("-------------------"+root.getPath()+"--------------------");
        for(File file : root.listFiles())         
            if(root.getPath().contains(aPatientNumber) ){
                 //System.out.println("-------------------"+root.getPath()+"-------------------- got it!");
            	if ( onlyDirectory == true){ 
            		 searchForDatFiles(file, datOnly,aPatientNumber,true,true);  
            	}
            	else 
            	{
            		searchForDatFiles(file, datOnly,aPatientNumber,true,false);  
            	}
                   
            }
            else{
                searchForDatFiles(file, datOnly,aPatientNumber,false,false);
                // System.out.println("-------------------"+root.getPath()+"-------------------- nope!");
            }
    }
    else if(root.isFile() && onlyDirectory == true ){
    	datOnly.add(root);	  
    	
    }
    else if(root.isFile() && root.getPath().indexOf( aPatientNumber) > -1 && ( root.getName().endsWith(".csv") || root.getName().endsWith(".sav") ||  root.getName().endsWith(".GDT") || root.getName().endsWith(".xls")  || root.getName().endsWith(".bin") || root.getName().endsWith(".xlsx") ) )
    {
        
        datOnly.add(root);
  
       
              
    }
    
}
   /**
    * Opens a scanner in the input folder, to read data from a file.
    * The file has to be a .csv.
    * 
    * @param aDestination the path of the file in the input folder.
    * @throws IOException
    */   
   public void openInputReader(String aDestination)throws IOException
   {
      Scanner aScanner = new Scanner(new File("./data/Input/"+aDestination+".csv")); 
      aScanners.add( aScanner);
      tellerS++;
   }
   
   
   /**
    * Opens a scanner in the output folder, to read data from it.
    * The file has to be a .csv.
    * 
    * @param aDestination the path of the file in the input folder.
    * @throws IOException
    */   
   public void openOutputReader(String aDestination)throws IOException
   {
      Scanner aScanner = new Scanner(new File(aDestination));
      aScanners.add( aScanner);
      tellerS++;
   }
   
   /**
    * Writes a string to the writer file.
    * 
    * @param aMessage a String containing what you want to write
    * @throws IOException
    */
   public void write(String aMessage) throws IOException
   {
      aFileWriters.get(tellerW).append(aMessage);
      
   }
   
   /**
    * Scans the scanned file for input.
    * Returns xxxx if nothing is found
    * 
    * @return a String containing what was scanned.
    * @throws IOException
    */
   public String scan() throws IOException
   {
      String scanString ="";
      if(aScanners.get(tellerS).hasNext())
      {
         scanString=  aScanners.get(tellerS).nextLine();
      }
      else
      {
        scanString ="xxxx";
      }
      return scanString;
   }
   
   /**
    * Closes a write file.
    * 
    * @throws IOException
    */
   public void closeW() throws IOException
   {
      aFileWriters.get(tellerW).close();
      aFileWriters.remove(tellerW);
      tellerW--;
      
   }
   
   /**
    * Closes a write file.
    * 
    * @throws IOException
    */
   public void closeS() throws IOException
   {
      aScanners.get(tellerS).close();
      tellerS--;
   }
   
   
   
   
   

}